﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import Engine.Managers.*;
    import Events.*;
    import Display.DialogUI.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.sim.*;
    import Display.*;
    import Modules.guide.*;
    import Modules.stats.experiments.*;
    import Classes.announcements.*;
    import Classes.Managers.*;
    import Init.PostInit.PostInitActions.*;
    import Modules.stats.types.*;
    import L10n.*;
    import com.zynga.skelly.util.*;

    public class StartUpDialogManager {

        public static const FIRST_TIME_VISIT_BONUS_DIALOG:int = 7;
        private static const FLASHVAR_EMPTYLOT:String = "emptyLot";
        private static const FLASHVAR_VALENTINE:String = "valentine";
        private static const FLASHVAR_ROllCALL:String = "rollCall";
        private static const ROLL_CALL_CHECK_IN:String = "checkIn";
        private static const ROLL_CALL_COLLECT:String = "collect";
        private static const BONUS_DIALOG_TIME:String = "FIRST_TIME_VISIT_BONUS_DIALOG";

        public static var dialogQueue:Array = [];
        private static var m_needsToInit:Boolean = true;
        private static var m_startUpParams:Array = [];
        private static var m_initResult:Object = null;
        public static var returnToDefaultHUD:Boolean = true;
        private static var helper:StartUpDialogHelper;

        public function StartUpDialogManager(){
            CheckInit();
        }
        private static function CheckInit():void{
            if (m_needsToInit){
                OnInit();
                m_needsToInit = false;
            };
        }
        private static function OnInit():void{
            dialogQueue = new Array();
            m_startUpParams = new Array();
            helper = new StartUpDialogHelper();
        }
        public static function StartUpDialogTrigger(_arg1:Object):void{
            CheckInit();
            m_initResult = _arg1;
            addStartupFlashvarActions();
            parceStartupFlashHotvarActions();
            addBuiltInStartupUtils();
            pumpDialogQueue();
        }
        public static function addStartupFlashvarActions():void{
            var _local1:Object = GlobalEngine.getFlashVar("startupType");
            if (_local1 != null){
                parseStartUpParams(_local1.toString());
            };
        }
        public static function parceStartupFlashHotvarActions():void{
            var _local2:String;
            var _local3:String;
            var _local4:String;
            var _local5:String;
            var _local6:int;
            var _local7:SamAdviceDialog;
            var _local8:StartupDialogBase;
            var _local1:Object = Global.flashHotParams["gameBar"];
            if (_local1 != null){
                _local2 = _local1.toString();
                _local3 = ("GameBar_" + _local2);
                _local4 = ZLoc.t("Dialogs", _local3);
                _local5 = "GameBar";
                _local6 = GenericPopup.TYPE_OK;
                _local7 = new SamAdviceDialog(_local4, "", _local6, null, _local5, "", true, 0);
                _local8 = new StartupDialogBase(_local7, "gameBar", false);
                dialogQueue.push(_local8);
            };
        }
        public static function addBuiltInStartupUtils():void{
            var _local1:Array = new Array(processAnnouncementConditions, processD2InviteNeighborConditions, processWarehouseCorruptionConditions, processFreeGiftSaleConditions, processFlashSaleConditions, processXpromo);
            while ((((dialogQueue.length == 0)) && ((_local1.length > 0)))) {
                (_local1.pop() as Function).call();
            };
        }
        private static function addD2InvitePopup():void{
            var strDialogMessage:* = ZLoc.t("Dialogs", "IdleAddNeighbors_text");
            var typeDialog:* = GenericDialogView.TYPE_ADDNEIGHBOR;
            var acceptHandler:* = function (_arg1:GenericPopupEvent):void{
                if (_arg1.button == GenericPopup.YES){
                    FrameManager.showTray("invite.php?ref=second_day_neighbor_prompt");
                };
            };
            var closeHandler:* = function (_arg1:Event=null):void{
                Global.player.setLastActivationTime("d2_invitefriendsdialog_seen", (GlobalEngine.getTimer() / 1000));
                StatsManager.count(StatsCounterType.PROMPTS, StatsKingdomType.D2NEIGHBOR);
            };
            var dialog:* = new SamAdviceDialog(strDialogMessage, "", typeDialog, acceptHandler, "IdleAddNeighbors", "", true, 0, "IdleAddNeighbors_button");
            var queueObject:* = new StartupDialogBase(dialog, "d2_invitefriendsdialog", false, closeHandler);
            dialogQueue.push(queueObject);
        }
        private static function parseStartUpParams(_arg1:String):void{
            var _local2:Array = _arg1.split(":");
            var _local3:String = _local2[0];
            var _local4 = 1;
            while (_local4 < _local2.length) {
                m_startUpParams.push(_local2[_local4]);
                _local4++;
            };
            if (_local3 == FLASHVAR_EMPTYLOT){
                if (_local2.length > 1){
                    dialogQueue.push(new StartupVisitActionDialog(String(_local2[1]), m_initResult.is_new, {}));
                };
            } else {
                if (_local3 == FLASHVAR_VALENTINE){
                    processIneligibleValentinesPlayer();
                } else {
                    if (_local3 == FLASHVAR_ROllCALL){
                        if (_local2.length > 3){
                            processRollCall(String(_local2[1]), String(_local2[2]), String(_local2[3]), m_initResult.is_new);
                        };
                    };
                };
            };
        }
        private static function processRollCall(_arg1:String, _arg2:String, _arg3:String, _arg4:Boolean):void{
            var _local5:String;
            var _local6:Player = Global.player.findFriendById(_arg1);
            if (_local6){
                _local5 = _local6.snUser.picture;
            };
            var _local7 = "RollCall_StartUpDialogA";
            var _local8 = "StartUpDialogA_text";
            var _local9 = "StartUpDialogB_text";
            var _local10 = "RollCall_CheckIn_StartUpDialogC_text";
            var _local11 = "RollCall_StartUpDialogD_text";
            switch (_arg2){
                case ROLL_CALL_CHECK_IN:
                    _local10 = "RollCall_CheckIn_StartUpDialogC_text";
                    break;
                case ROLL_CALL_COLLECT:
                default:
                    _local10 = "RollCall_Collect_StartUpDialogC_text";
            };
            var _local12:Item = Global.gameSettings.getItemByName(_arg3);
            var _local13 = "";
            if (_local12){
                _local13 = _local12.localizedName;
            };
            dialogQueue.push(new StartupVisitActionDialog(_arg1, _arg4, {buildingName:_local13}, _local5, _local7, _local8, _local9, _local10, _local11, false));
        }
        private static function processIneligibleValentinesPlayer():void{
            var _local1:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_VDAY_2011);
            if (_local1 == 1){
                if (((!(ValentineManager.m_shownIneligible)) && (!(ValentineManager.checkEligibility())))){
                    ValentineManager.m_shownIneligible = true;
                    dialogQueue.push(new StartupMessage("ValUI", ZLoc.t("Dialogs", "ValUI_StartupIneligible"), ExperimentDefinitions.EXPERIMENT_VDAY_2011));
                };
            };
        }
        private static function processFreeGiftSaleConditions():void{
            if (Global.freeGiftSaleManager.canShowSale() === true){
                dialogQueue.push(new StartupDialogBase(Global.freeGiftSaleManager.getStartupDialog(), "FreeGiftOffer", false, null));
            };
        }
        private static function processWarehouseCorruptionConditions():void{
            var _local1:Number = Global.player.getFlag("double_warehouse_check").value;
            switch (_local1){
                case -1:
                    UI.displayMessage(ZLoc.t("Dialogs", "ItemStorage_CorruptionFix"));
                    Global.player.getFlag("double_warehouse_check").setAndSave(-2);
                    break;
                case 0:
                    break;
                case -2:
                    break;
                case 1:
                    UI.displayMessage(ZLoc.t("Dialogs", "ItemStorage_CorruptionFix"));
                    Global.player.getFlag("double_warehouse_check").setAndSave(-2);
                    break;
            };
        }
        private static function processFlashSaleConditions():void{
            if (((Global.flashSaleManager.isFlashSaleAvailable) && (!(Global.flashSaleManager.isCooldownActive)))){
                dialogQueue.push(new StartupDialogBase(Global.flashSaleManager.getStartupDialog(), "FlashSale", false, null));
            };
        }
        private static function processD2InviteNeighborConditions():void{
            var _local1:int = Global.player.getLastActivationTime("d2_invitefriendsdialog_timer");
            var _local2 = !((Global.player.getLastActivationTime("d2_invitefriendsdialog_seen") == -1));
            var _local3:int = Global.gameSettings.getInt("inviteNeighborReminderPeriod", ((60 * 60) * 24));
            if (((((((!(_local2)) && (!(Global.player.isNewPlayer)))) && ((_local1 > 0)))) && ((_local1 < ((GlobalEngine.getTimer() / 1000) - _local3))))){
                addD2InvitePopup();
            };
        }
        private static function processXpromo():void{
            var _local1:StartupDialogBase = InGameXPromoManager.getInstance().getStartupDialog();
            if (((_local1) && (_local1.shouldQueue))){
                StartUpDialogManager.dialogQueue.push(_local1);
            };
        }
        protected static function processAnnouncementConditions():void{
            var _local2:Function;
            if (m_initResult.is_new){
                return;
            };
            var _local1:AnnouncementData = Global.announcementManager.getNextAnnouncement();
            if (_local1){
                _local2 = null;
                if (_local1.view.closeCallback){
                    _local2 = Curry.curry(helper.getCallback(_local1.view.closeCallback.name), _local1.view.closeCallback.params);
                };
                dialogQueue.push(new AnnouncementDialog(_local1, _local2));
                return;
            };
        }
        public static function displayAnnouncement(_arg1:String):void{
            var _local2:AnnouncementData;
            var _local3:Function;
            var _local4:AnnouncementDialog;
            for each (_local2 in Global.gameSettings.getAnnouncements()) {
                if (_local2.id == _arg1){
                    _local3 = null;
                    if (_local2.view.closeCallback){
                        _local3 = Curry.curry(helper.getCallback(_local2.view.closeCallback.name), _local2.view.closeCallback.params);
                    };
                    _local4 = new AnnouncementDialog(_local2, _local3);
                    _local4.showStandalone();
                };
            };
        }
        public static function pumpDialogQueue():void{
            var _local1:*;
            if (dialogQueue.length == 0){
                doFinalStartUpTasks();
            } else {
                _local1 = dialogQueue.pop();
                if (("show" in _local1)){
                    _local1.show();
                } else {
                    GlobalEngine.log("Error", "An element that doesn't have a show method was put in the startupdialogqueue.");
                };
            };
        }
        public static function clearDialogQueue():void{
            dialogQueue = new Array();
        }
        private static function areHolidayClothingQuestsActive():Boolean{
            var _local1:Boolean = Global.questManager.isQuestActive("qm_holiday_snow_ground");
            var _local2:Boolean = Global.questManager.isQuestActive("qm_holiday_snowandlights");
            var _local3:Boolean = Global.questManager.isQuestActive("qm_holiday_winterclothes");
            return (((((_local1) || (_local2))) || (_local3)));
        }
        private static function forceHolidayClothingQuestWrapUp():void{
            Global.questManager.expireSpecificQuest("qm_holiday_snow_ground");
            Global.questManager.expireSpecificQuest("qm_holiday_snowandlights");
            Global.questManager.expireSpecificQuest("qm_holiday_winterclothes");
            GameTransactionManager.addTransaction(new THolidayClothingRemoveAllQuests());
        }
        private static function doHolidayClothingQuestCleanup():void{
            if (areHolidayClothingQuestsActive()){
                forceHolidayClothingQuestWrapUp();
            };
        }
        public static function areChineseNewYearQuestsActive():Boolean{
            var _local1:Boolean = Global.questManager.isQuestActive("qm_cny1");
            var _local2:Boolean = Global.questManager.isQuestActive("qm_cny2");
            var _local3:Boolean = Global.questManager.isQuestActive("qm_cny3");
            return (((((_local1) || (_local2))) || (_local3)));
        }
        public static function forceChineseNewYearQuestsWrapUp():void{
            Global.questManager.expireSpecificQuest("qm_cny1");
            Global.questManager.expireSpecificQuest("qm_cny2");
            Global.questManager.expireSpecificQuest("qm_cny3");
            GameTransactionManager.addTransaction(new TChineseNewYearRemoveAllQuests());
        }
        public static function doChineseNewYearQuestCleanup():void{
            if (areChineseNewYearQuestsActive()){
                forceChineseNewYearQuestsWrapUp();
            };
        }
        private static function doFinalStartUpTasks():void{
            if (m_initResult.is_new){
                Global.guide.loadFirstStepAssets();
                if (LocaleHelper.localSetting.sns.experiment_FTUE_enable){
                    if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CTTC_FTUE_GATE) == 1){
                        Global.guide.notify(GuideUtils.FIRST_STEP_CTTC_ALPHA);
                    } else {
                        Global.guide.notify(GuideUtils.FIRST_STEP);
                    };
                } else {
                    Global.guide.notify(GuideUtils.FIRST_STEP);
                };
            } else {
                Global.player.setAllowQuests(true);
                if (returnToDefaultHUD){
                    Global.hud.refreshGoodsHUD();
                };
            };
            if (m_initResult.player.quests != null){
                Global.questManager.loadQuestData(m_initResult.player.quests);
            } else {
                Global.questManager.loadQuestData({name:"new"});
            };
            Global.questManager.startUp();
        }
        public static function doAfterQuestSystemStartUp():void{
            HolidayTree.wrapUpHolidayQuests();
            doHolidayClothingQuestCleanup();
            doChineseNewYearQuestCleanup();
            GameTransactionManager.addTransaction(new TStartManualQuest("neighbor_gate_quest_3"), true);
            GameTransactionManager.addTransaction(new TStartManualQuest("qm_storage_warehouse"), true);
            GameTransactionManager.addTransaction(new TStartManualQuest("qm_visitor_center"), true);
            GameTransactionManager.addTransaction(new TStartManualQuest("qm_retail1"), true);
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_FACTORY) == ExperimentDefinitions.FACTORY_ENABLED){
            };
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CLERK_OFFICE) == ExperimentDefinitions.CLERK_OFFICE_ENABLED){
                GameTransactionManager.addTransaction(new TStartManualQuest("qm_clerk_office"), true);
            };
            GameTransactionManager.addTransaction(new TStartManualQuest("qm_st_pattys_1"), true);
            GameTransactionManager.addTransaction(new TStartManualQuest("qm_cincodemayo_1"), true);
            GameTransactionManager.addTransaction(new TStartManualQuest("qm_upgrade_neighborBUS_bakery"), true);
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_MALLS) == ExperimentDefinitions.MALLS_ENABLED){
                GameTransactionManager.addTransaction(new TStartManualQuest("qf_mall_construction"), true);
            };
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CRUISESHIP) == true){
            };
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CTTC_PEDDLERCENTER) == ExperimentDefinitions.EXPERIMENT_CTTC_PEDDLERCENTER_VALUE){
                GameTransactionManager.addTransaction(new TStartManualQuest("qm_peddler_center"), true);
            };
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_BUSINESS_UPGRADES) == true){
                GameTransactionManager.addTransaction(new TStartManualQuest("qm_upgrade_business"), true);
            };
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_ZOO) == ExperimentDefinitions.ZOO_ENABLED){
            };
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_BRIDGE) == ExperimentDefinitions.EXPERIMENT_BRIDGE_SHOW){
            };
            if (Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_SPRING_DAU) == true){
                GameTransactionManager.addTransaction(new TStartManualQuest("spring_party_quest"), true);
            };
        }

    }
}//package Classes 
